package com.doubao.user.controller;

import com.doubao.common.result.Result;
import com.doubao.user.dto.response.UserInfoResponse;
import com.doubao.user.dto.response.UserStatisticsResponse;
import com.doubao.user.service.UserDailyTokenService;
import com.doubao.user.service.UserService;
import com.doubao.user.service.UserStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.List;
import java.util.Map;

/**
 * 用户统计控制器
 */
@RestController
@RequestMapping("/statistics")
@RequiredArgsConstructor
@Tag(name = "用户统计管理", description = "用户统计相关接口")
public class UserStatisticsController {

    private final UserService userService;
    private final UserStatisticsService userStatisticsService;
    private final UserDailyTokenService userDailyTokenService;

    /**
     * 获取用户统计信息
     */
    @GetMapping("")
    @Operation(summary = "获取用户统计信息", description = "获取当前登录用户的统计信息")
    public Result<UserStatisticsResponse> getUserStatistics() {
        UserInfoResponse userInfo = userService.getCurrentUserInfo();
        return Result.success(userStatisticsService.getUserStatistics(userInfo.getId()));
    }

    /**
     * 获取用户今日token使用情况
     */
    @GetMapping("/token/today")
    @Operation(summary = "获取用户今日token使用情况", description = "获取当前登录用户今日token使用情况")
    public Result<Map<String, Object>> getTodayTokenUsage() {
        UserInfoResponse userInfo = userService.getCurrentUserInfo();
        Long userId = userInfo.getId();
        int used = userDailyTokenService.getUsedTokens(userId, LocalDate.now());
        int available = userDailyTokenService.getAvailableTokens(userId);

        Map<String, Object> result = Map.of(
                "used", used,
                "available", available,
                "limit", userInfo.getDailyTokensLimit(),
                "date", LocalDate.now().toString()
        );

        return Result.success(result);
    }

    /**
     * 获取用户某月token使用趋势
     */
    @GetMapping("/token/trend/{year}/{month}")
    @Operation(summary = "获取用户某月token使用趋势", description = "获取用户指定年月的token使用趋势")
    public Result<List<Map<String, Object>>> getMonthlyTokenTrend(
            @PathVariable("year") int year,
            @PathVariable("month") int month) {
        UserInfoResponse userInfo = userService.getCurrentUserInfo();
        List<Map<String, Object>> trend = userDailyTokenService.getMonthlyTrend(userInfo.getId(), year, month);
        return Result.success(trend);
    }

    /**
     * 获取用户当月token使用趋势
     */
    @GetMapping("/token/trend/current")
    @Operation(summary = "获取用户当月token使用趋势", description = "获取当前登录用户当月token使用趋势")
    public Result<List<Map<String, Object>>> getCurrentMonthTokenTrend() {
        UserInfoResponse userInfo = userService.getCurrentUserInfo();
        LocalDate now = LocalDate.now();
        List<Map<String, Object>> trend = userDailyTokenService.getMonthlyTrend(
                userInfo.getId(), now.getYear(), now.getMonthValue());
        return Result.success(trend);
    }

    /**
     * 获取用户最近7天token使用趋势
     */
    @GetMapping("/token/trend/recent")
    @Operation(summary = "获取用户最近7天token使用趋势", description = "获取当前登录用户最近7天token使用趋势")
    public Result<Object> getRecentTokenTrend() {
        UserInfoResponse userInfo = userService.getCurrentUserInfo();
        Object trend = userStatisticsService.getTokenUsageTrend(userInfo.getId());
        return Result.success(trend);
    }
}